<template>
    <div>
        <el-form status-icon ref="form" :model="form" :rules="rules" label-width="80px" class="register-box">
        <h3 class="login-title">欢迎注册</h3>
            <el-form-item label="账号" prop="username">
                <el-input type="text" placeholder="请输入用户名" v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" placeholder="请输入密码" v-model="form.password"></el-input>
            </el-form-item>
            <el-button type="primary" @click="submitForm('form')">立即创建</el-button>
            <el-button @click="resetForm('form')">重置</el-button>
            <el-button  class="gologin" @click="gologin()">我要登陆</el-button>
        </el-form>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        name:'XiaoRegister',
        data() {
            return {
                form:{
                    username:'',
                    password:'',
                },
                rules:{
                    username:[{
                        required: true,
                        message:'请输入用户名',
                        trigger:'blur'
                    }],
                    password:[{
                        required: true,
                        message:'请输入密码',
                        trigger:'blur'
                    }],
                }
            }
        },
        methods: {
            submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    console.log(this.form.username,'@@@',this.form.password);
                    this.$addr.get(`users/reg?username=${this.form.username}&password=${this.form.password}`).then(
                        response=>{
                            console.log('请求成功了',response)
                            if(response.data.state == 4000){
                                this.$message({
                                    message: '用户名被占用',
                                    type: 'error'
                                });
                            }else if(response.data.state == 200){
                                this.$router.push({name:'xLogin',
                                            params:{'username':this.form.username}})
                            }
                        },
                        error=>{
                            this.$message({
                                message: '呕吼请求失败了',
                                type: 'warning'
                            });
                            console.log('请求失败了',error.message)
                        }
                    )
                }
            })},
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            gologin(){
                this.$router.push({name:'xLogin'})
            }
        },
    }
</script>

<style lang="scss" scoped>
    .register-box{
        width: 350px;
        margin: 150px auto;
        border: 1px solid #DCDFE6;
        padding: 20px;
        border-radius: 5px;
        box-shadow: 0 0 30px #DCDFE6;
    }
    .login-title{
        text-align: center;
    }
    .gologin{
        background-color: rgb(105, 211, 214);
    }
</style>
